/*
 * @Author: Jiraiya
 * @Date: 2020-05-26 16:52:59
 * @LastEditors: Jiraiya
 * @LastEditTime: 2020-05-26 17:33:55
 * @Description: 
 */ 

import compose from './compose';

export default function applyMiddleware(...middlewares){
    return function rewriteCreateStore(oldCreateStore){
        return function newCreateStore(reducer, initState){
            const store = oldCreateStore(reducer, initState);
            const simpleStore = {getState: store.getState};
            const chain = middlewares.map(middleware=>middleware(simpleStore));

            const dispatch = compose(...chain)(store.dispatch);

            return {
                ...store,
                dispatch
            }
        }
    }
}
